#include <algorithm>
#include <cstring>
#include <iostream>

int n;
int cnts[20];
int ans;

int to_rank(int a, int b) {
    if (a == 0)
        return 16 + b - 1;
    if (a == 1)
        return 14;
    if (a == 2)
        return 15;
    return a;
}

void dfs(int moves) {
    if (moves >= ans) {
        return;
    }
    bool empty = true;
    for (int i = 3; i <= 17; ++i) {
        if (cnts[i] > 0) {
            empty = false;
            break;
        }
    }
    if (empty) {
        ans = std::min(ans, moves);
        return;
    }

    for (int len = 12; len >= 5; --len) {
        for (int i = 3; i <= 14 - len + 1; ++i) {
            bool pos = true;
            for (int k = i; k < i + len; ++k) {
                if (cnts[k] == 0) {
                    pos = false;
                    break;
                }
            }
            if (pos) {
                for (int k = i; k < i + len; ++k)
                    cnts[k]--;
                dfs(moves + 1);
                for (int k = i; k < i + len; ++k)
                    cnts[k]++;
            }
        }
    }

    for (int len = 10; len >= 3; --len) {
        for (int i = 3; i <= 14 - len + 1; ++i) {
            bool pos = true;
            for (int k = i; k < i + len; ++k) {
                if (cnts[k] < 2) {
                    pos = false;
                    break;
                }
            }
            if (pos) {
                for (int k = i; k < i + len; ++k)
                    cnts[k] -= 2;
                dfs(moves + 1);
                for (int k = i; k < i + len; ++k)
                    cnts[k] += 2;
            }
        }
    }

    for (int len = 6; len >= 2; --len) {
        for (int i = 3; i <= 14 - len + 1; ++i) {
            
        }
    }

    for (int i = 3; i <= 15; ++i) {
        if (cnts[i] >= 3) {
            for (int k = 3; k <= 17; ++k) {
                if (k != i && cnts[k] >= 1) {
                    cnts[i] -= 3;
                    cnts[k] -= 1;
                    dfs(moves + 1);
                    cnts[i] += 3;
                    cnts[k] += 1;
                }
            }
        }

        if (cnts[i] >= 3) {
            for (int k = 3; k <= 15; ++k) {
                if (k != i && cnts[k] >= 2) {
                    cnts[i] -= 3;
                    cnts[k] -= 2;
                    dfs(moves + 1);
                    cnts[i] += 3;
                    cnts[k] += 2;
                }
            }
        }

        if (cnts[i] >= 4) {
            
        }

        if (cnts[i] >= 4) {
            
        }
    }

    if (cnts[16] > 0 && cnts[17] > 0) {
        cnts[16]--;
        cnts[17]--;
        dfs(moves + 1);
        cnts[16]++;
        cnts[17]++;
    }

    for (int i = 3; i <= 17; ++i) {
        if (cnts[i] >= 4) {
            cnts[i] -= 4;
            dfs(moves + 1);
            cnts[i] += 4;
        }
        if (cnts[i] >= 3) {
            cnts[i] -= 3;
            dfs(moves + 1);
            cnts[i] += 3;
        }
        if (cnts[i] >= 2) {
            cnts[i] -= 2;
            dfs(moves + 1);
            cnts[i] += 2;
        }
        if (cnts[i] >= 1) {
            cnts[i] -= 1;
            dfs(moves + 1);
            cnts[i] += 1;
        }
    }
}

void solve() {
    std::cin >> n;
    memset(cnts, 0, sizeof(cnts));
    for (int i = 0; i < n; ++i) {
        int a, b;
        std::cin >> a >> b;
        cnts[to_rank(a, b)]++;
    }

    ans = n;
    dfs(0);
    std::cout << ans << "\n";
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    int T;
    std::cin >> T >> n;
    while (T--) {
        solve();
    }
}
// TODO to done